
package regAlloc;

import java.util.HashSet;
import java.util.Set;

import temp.Temp;

public class TempGraph {
        private Set<TempNode> nodes = new HashSet<TempNode>();
        
        public Set<TempNode> getNodes() {
                return nodes;
        }
        public void addEdge(Temp t1, Temp t2) {                 
                TempNode tn1 = haveNode(t1);
                TempNode tn2 = haveNode(t2);
                if(tn1 == null) {
                        tn1 = new TempNode(t1);
                        this.nodes.add(tn1);
                }
                if(tn2 == null) {
                        tn2 = new TempNode(t2);
                        this.nodes.add(tn2);
                }
                tn1.addEdge(tn2);
                tn2.addEdge(tn1);               
        }
        
        public TempNode haveNode(Temp t) {
                TempNode result = null;
                label : for(TempNode t1 : this.nodes) {
                        if(t1.getValue().equals(t)) {
                                result = t1;
                                break label;
                        }
                }
                return result;
        }
        
        @Override
        public String toString() {
                StringBuffer sb = new StringBuffer();
                for(TempNode t : nodes) {
                        sb.append(t.toString());
                }               
                return sb.toString();
        }
}